<?php 
require_once (APPPATH.'controllers/base_controllers/base_validation_controller.php');

class Banquet_validation extends Base_validation_controller {

    public function __construct() {
        parent::__construct();
        
        $this->load->library('validation');
    }
    
    public function validate_registration($eventID) {
        if (is_numeric($this->input->post('numadultreg')))
            $numadults = intval($this->input->post('numadultreg'));
        else
            $numadults = 0;
            
        if (is_numeric($this->input->post('numchildreg')))
            $numchildren = intval($this->input->post('numchildreg'));
        else
            $numchildren = 0;
            
        if (is_numeric($this->input->post('donationamount')))
            $donation = floatval($this->input->post('donationamount'));
        else
            $donation = 0;
            
        $totfee = 50 * $numadults + 20 * $numchildren + $donation;
        if ($totfee < 0.01) {
            echo("<b>MUST SPECIFY ATTENDEES AND/OR A DONATION</b>");
            die;
        }
        
        switch ($eventID) {
            case 1:
                $rules['lastname'] = "trim|required|max_length[16]";
                $rules['firstname'] = "trim|required|max_length[16]";
                $rules['email'] = "trim|required|valid_email|matches[emailConfirm]|max_length[127]";
                //$rules['numadultreg'] = "trim|required|min_length[1]|max_length[2]";
                //$rules['numchildreg'] = "trim|required|min_length[1]|max_length[2]";
                $rules['phoneNumber'] = "trim|required|callback_valid_phone_check|max_length[12]";
                $rules['address'] = "trim|alpha_address|min_length[1]|max_length[127]";
                $rules['city'] = "trim|alpha_dash|min_length[1]|max_length[63]";
                $rules['state'] = "trim|max_length[31]";
                $rules['zipCode'] = "trim|callback_valid_zip_code_check|max_length[10]";

                
                $this->validation->set_rules($rules);
                $validationPassed = $this->validation->run();
                
                if ($validationPassed) {
                    echo('VALIDATION_SUCCESS');
                } else {
                    echo($this->clean_validation_errors_validate_registration($this->validation->error_string, $eventID));
                }
                break;
        }
        
        die;
    }
    
    public function validate_event_release_check()
	{
        $this->load->model('proxies/Walk_proxy');
        $this->load->model('proxies/annualbanquet/Annualbanquet_proxy');
        
        //Get check info
        $curyear = $this->Walk_proxy->getCurrentYear();
        $RegIdArray = $this->Annualbanquet_proxy->getEventUnpaidCheckRegId($curyear);
        
        if (count($RegIdArray) > 0) {
            $flg = false;
            //CheckInfo
            for ($i = 0; $i < count($RegIdArray); $i++) {
                $checknum = $this->input->post('svid'.$RegIdArray[$i]);
                if ($checknum != "") {
                    $rules['bqid'.$RegIdArray[$i]] = "trim|required|alpha_dash|min_length[1]|max_length[16]";
                    $flg = true;
                }
            }
            
            if ($flg) {
                $this->validation->set_rules($rules);
                $validationPassed = $this->validation->run();
            } else
                $validationPassed = true;
        } else {
            $validationPassed = true;
        }
        
        if ($validationPassed) {
            echo('VALIDATION_SUCCESS');
        } else {
            echo($this->clean_validation_errors_validate_event_release_check($this->validation->error_string, $RegIdArray));
        }
        
        die;
    }
    
    public function validate_admin_edit_reg() {
        if (is_numeric($this->input->post('numadultreg')))
            $numadults = intval($this->input->post('numadultreg'));
        else
            $numadults = 0;
            
        if (is_numeric($this->input->post('numchildreg')))
            $numchildren = intval($this->input->post('numchildreg'));
        else
            $numchildren = 0;
            
        if (is_numeric($this->input->post('donationamount')))
            $donation = floatval($this->input->post('donationamount'));
        else
            $donation = 0;
            
        $totfee = 50 * $numadults + 20 * $numchildren + $donation;
        if ($totfee < 0.01) {
            echo("<b>MUST SPECIFY ATTENDEES AND/OR A DONATION</b>");
            die;
        }
        
        $rules['lastname'] = "trim|required|max_length[16]";
        $rules['firstname'] = "trim|required|max_length[16]";
        $rules['email'] = "trim|required|valid_email|matches[emailConfirm]|max_length[127]";
        $rules['numadultreg'] = "trim|integer|max_length[2]";
        $rules['numchildreg'] = "trim|integer|max_length[2]";
        $rules['phone'] = "trim|required|callback_valid_phone_check|max_length[12]";
        $rules['address'] = "trim|required|alpha_address|min_length[1]|max_length[127]";
        $rules['city'] = "trim|required|alpha_dash|min_length[1]|max_length[63]";
        $rules['state'] = "trim|required";
        $rules['zip'] = "trim|required|callback_valid_zip_code_check|max_length[10]";
        $rules['paymenttype'] = "trim|required|min_length[4]|max_length[16]";
        $rules['donationamount'] = "trim|integer|max_length[10]";
        //$rules['checknum'] = "trim|required|min_length[1]|max_length[16]";
        //$rules['paytxn'] = "trim|required|min_length[1]|max_length[64]";
        //$rules['paymentstatus'] = "trim|required|min_length[1]|max_length[1]";
        //$rules['comments'] = "trim|required|min_length[1]|max_length[1024]";

        
        $this->validation->set_rules($rules);
        $validationPassed = $this->validation->run();
        
        if ($validationPassed) {
            echo('VALIDATION_SUCCESS');
        } else {
            echo($this->clean_validation_errors_validate_admin_edit_reg($this->validation->error_string));
        }
        
        die;
    }
    
    private function clean_validation_errors_validate_registration($valErrors, $eventID) {
        switch ($eventID) {
            case 1:
                $valErrors = str_replace("firstname", "First name", $valErrors);
                $valErrors = str_replace("lastname", "Last name", $valErrors);
                $valErrors = str_replace("email", "Email", $valErrors);
                $valErrors = str_replace("emailConfirm", "Confirm Email", $valErrors);
                $valErrors = str_replace("phone", "Phone Number", $valErrors);
                $valErrors = str_replace("address", "Address", $valErrors);
                $valErrors = str_replace("city", "City", $valErrors);
                $valErrors = str_replace("state", "State", $valErrors);
                $valErrors = str_replace("zipCode", "Zip Code", $valErrors);
                //$valErrors = str_replace("numadultreg", "Number of adults", $valErrors);
                //$valErrors = str_replace("numchildtreg", "Number of children", $valErrors);
                break;
        }
        
        return $valErrors;
    }
    
    private function clean_validation_errors_validate_event_release_check($valErrors, $RegIdArray) {
    
        if (count($RegIdArray) > 0) {
            //CheckInfo
            for ($i = 0; $i < count($RegIdArray); $i++) {
                $checknum = $this->input->post('bqid'.$RegIdArray[$i]);
                if ($checknum != "") {
                    $valErrors = str_replace("'bqid'.$RegIdArray[$i]", "Check #", $valErrors);
                }
            }
        }
        
        return $valErrors;
    }
    
    private function clean_validation_errors_validate_admin_edit_reg($valErrors) {
        $valErrors = str_replace("firstname", "First name", $valErrors);
        $valErrors = str_replace("lastname", "Last name", $valErrors);
        $valErrors = str_replace("email", "Email", $valErrors);
        $valErrors = str_replace("emailConfirm", "Confirm Email", $valErrors);
        $valErrors = str_replace("phone", "Phone Number", $valErrors);
        $valErrors = str_replace("address", "Address", $valErrors);
        $valErrors = str_replace("city", "City", $valErrors);
        $valErrors = str_replace("state", "State", $valErrors);
        $valErrors = str_replace("zip", "Zip Code", $valErrors);
        $valErrors = str_replace("numadultreg", "Number adults", $valErrors);
        $valErrors = str_replace("numchildreg", "Number children", $valErrors);
        $valErrors = str_replace("donationamount", "Donation", $valErrors);
        $valErrors = str_replace("paymenttype", "Payment Type", $valErrors);
        //$valErrors = str_replace("checknum", "Check #", $valErrors);
        //$valErrors = str_replace("paytxn", "CC TXN ID", $valErrors);
        //$valErrors = str_replace("paymentstatus", "Payment Status", $valErrors);
        //$valErrors = str_replace("comments", "Comments", $valErrors);
        
        return $valErrors;
    }
}

?>
